﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GabageFactory.Models;

namespace GabageFactory.Controllers
{
    [Authorize]
    public class AnalyzeController : BaseController
    {
        private StackReposity stackReposity;

        private StackRainfallReposity stackRainfallReposity;

        private MaterialReposity materialReposity;

        private DikeReposity dikeReposity;

        private PoolReposity poolReposity;

        public AnalyzeController()
        {
            stackReposity = new StackReposity();
            stackRainfallReposity = new StackRainfallReposity();
            materialReposity = new MaterialReposity();
            dikeReposity = new DikeReposity();
            poolReposity = new PoolReposity();
        }

        //
        // GET: /Analyze/
        public ActionResult Index(int id)
        {
            ActionResult result = AllowForRole(RoleEnum.Analyzer);
            if (result != null)
            {
                return result;
            }

            Stack stack = stackReposity.GetStackById(id);
            ShowFactoryList(stack.FactoryId);
            ViewData["id"] = id;
            return View(stack);
        }

        //
        // GET: /Analyze/
        public ActionResult Weather(int id)
        {
            ActionResult result = AllowForRole(RoleEnum.Analyzer);
            if (result != null)
            {
                return result;
            }

            IEnumerable<Stack_Rainfall_Evaporation_Base_info> rainfalls = stackRainfallReposity.ListRainfalls(id);
            Stack_Rainfall_Evaporation_Base_info[] rainfallArray = rainfalls.ToArray();
            Weather weather = new Weather();
            for (int i = 0; i < 12; i++)
            {
                if (i < rainfallArray.Length)
                {
                    weather.Months[i] = rainfallArray[i];
                }
                else
                {
                    Stack_Rainfall_Evaporation_Base_info rainfall = new Stack_Rainfall_Evaporation_Base_info();
                    rainfall.stack_id = id;
                    rainfall.month_no = i + 1;
                    weather.Months[i] = rainfall;
                }
            }
            ViewData["id"] = id;

            return View(weather);
        }
    }
}
